package com.wl.api.service.business.impl;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.api.client.util.Objects;
import com.wl.api.common.utils.BeanUtils;
import com.wl.api.common.utils.ObjectUtil;
import com.wl.api.common.utils.RWYException;
import com.wl.api.common.utils.constant.ClientConstant;
import com.wl.api.dto.business.CustomerFundDetailDTO;
import com.wl.api.mapper.business.CustomerFundMapper;
import com.wl.api.model.business.CustomerFund;
import com.wl.api.model.business.CustomerFundDetail;
import com.wl.api.service.business.ICustomerFundDetailService;
import com.wl.api.service.business.ICustomerFundService;

import lombok.extern.log4j.Log4j;

/**
 *
 * CustomerFund 表数据服务层接口实现类
 *
 */
@Service
@Log4j
public class CustomerFundServiceImpl extends ServiceImpl<CustomerFundMapper, CustomerFund> implements ICustomerFundService {

	@Autowired
	ICustomerFundDetailService fundDetailService;
	
	@Transactional
	public synchronized void updateCustomerFund(CustomerFundDetailDTO param) throws Exception {
		Assert.isTrue(param != null && param.getStatus() != null && param.getCustId() != null && param.getFundDetailId() != null && param.getCheckUserId() != null);
		CustomerFundDetail detail = fundDetailService.selectById(param.getFundDetailId());
		if (detail == null || !Objects.equal(detail.getCustId(), param.getCustId()) 
				|| !Objects.equal(ClientConstant.audit_status_confirming, detail.getStatus())
				|| ObjectUtil.isEmptyOrLtZero(detail.getAmount())
				) {
			log.error(" Abnormal  the administrator trying to modify the funds... admin user_id :" + param.getCheckUserId() +": update customer+"+ param.getCustId()+" funds" + "fund detail id:"+ param.getFundDetailId());
			throw RWYException.create(403);
		}
		boolean result =  fundDetailService.updateById(CustomerFundDetail.builder().fundDetailId(param.getFundDetailId()).status(param.getStatus()).remark(param.getRemark()).checkTime(param.getCheckTime()).checkUserName(param.getCheckUserName()).checkUserId(param.getCheckUserId()).build());
		if (result && param.getStatus() == ClientConstant.audit_status_confirmed) {
			CustomerFund fund = selectOne(new EntityWrapper<CustomerFund>().where("cust_id={0}", param.getCustId()));
			if (java.util.Objects.isNull(fund)) {
				CustomerFund entity = CustomerFund.builder().custId(param.getCustId())
						              .currentAmount(detail.getAmount()).beforeAmount(new BigDecimal("0.00")).totalAmount(detail.getAmount()).updateTime(new Date()).build();
				insert(entity);
			} else {
				CustomerFund entity = CustomerFund.builder().fundId(fund.getFundId())
						              .beforeAmount(fund.getCurrentAmount()).currentAmount(fund.getCurrentAmount().add(detail.getAmount()))
									  .totalAmount(fund.getTotalAmount().add(detail.getAmount())).build();
				updateById(entity);
			}
		}
	}
	


}